package cn.zzm.core.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.jms.core.JmsTemplate;

/**
 * TODO(jms工具类).
 *
 * <p>TODO(jms工具类).
 *
 * <p>版权所有:Copyright (c) 2019<br/>
 *
 * @author 周昭名
 * @version V1.0
 * @since 2019/7/1 17:45
 */
@Slf4j
public class ActiveMQUtil {

    /**
      *
      *TODO(发送消息到topic).
      *
      *<p>TODO(发送消息到topic).
      *
      *@return
      *@author 周昭名
      *@since 2019/7/1 17:51
    */
    public static void sendTopicMsg(JmsTemplate jmsTemplate,String topicName, String msg){
        try{
            log.info("发送消息到topic，目的地：{},消息内容{}：",topicName,msg);
            ActiveMQTopic topic = new ActiveMQTopic();
            topic.setPhysicalName(topicName);
            jmsTemplate.convertAndSend(topic,msg);
            log.info("消息发送成功");
        }catch (Exception e){
            log.error("消息发送失败");
            e.printStackTrace();
        }
    }

    /**
     *
     *TODO(发送消息到queue).
     *
     *<p>TODO(发送消息到queue).
     *
     *@return
     *@author 周昭名
     *@since 2019/7/1 17:51
     */
    public static void sendQueueMsg(JmsTemplate jmsTemplate,String queueName, String msg){
        try {
            log.info("发送消息到queue，目的地：{},消息内容{}：",queueName,msg);
            ActiveMQQueue queue = new ActiveMQQueue();
            queue.setPhysicalName(queueName);
            jmsTemplate.convertAndSend(queue,msg);
            log.info("消息发送成功");
        } catch (Exception e) {
            log.error("消息发送失败");
            e.printStackTrace();
        }

    }
}
